/*
 * @Author: Jone XIn
 * @Date: 2022-01-10 23:44:59
 * @LastEditTime: 2022-01-10 23:48:06
 * @Description: 通用工具函数
 */
const os = require('os');

/**
 * 获取本机IP
 * @returns Ip
 */
const getLocalIp = () => {
    var interfaces = os.networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
}

/**
 * @param {时间戳} date
 * @returns 格式化后的时间
 */
function formatDate(date) {
    let time = new Date(date);
    let minites = time.getMinutes() < 10 ? `0${time.getMinutes()}` : time.getMinutes();
    let seconds = time.getSeconds() < 10 ? `0${time.getSeconds()}` : time.getSeconds();
    return `${time.toLocaleDateString()} ${time.getHours()}:${minites}:${seconds}`;
}

module.exports = {
    getLocalIp,
    formatDate
};